﻿<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
/********************
 *@param function:OplusCms 前臺 頁面管理
 *@param datetime 2012-01-05
 *@param author solgh
 ********************/
require APPPATH.'/libraries/REST_Controller.php';
class Orders extends REST_Controller{
    public function __construct(){
        parent::__construct();

    }
    public function yprint_get(){
        $this->load->model('products_mdl');
        $this->load->model('orders_mdl');
        $this->load->model('ordersproduct_mdl');
        $this->load->model('specs_mdl');
        $order_id = $this->input->get_post('order_id');

        if(isset($order_id) && $order_id){
            $orderinfo = $this->orders_mdl->get_orders_by_id($order_id);
            $goodsinfo = $this->ordersproduct_mdl->_get_order_product($order_id);

            $lists = $this->get_list();
            foreach($lists as $key=>$arr){
                foreach($arr as $v){
                    if($v['key']=='printname'){
                        $printname = $v['value'];
                    }else if($v['key']=='printkey'){
                        $printkey = $v['value'];
                    } else if($v['key']=='telephone'){
                        $phone = $v['value'];
                    }else if($v['key']=='address'){
                        $address = $v['value'];
                    }
                }

            }

            $this->load->library('yprint');

            $content = "<FS2>".$orderinfo['table_num']."</FS2>";
            $content .= "\n";
            $content .= "<FS2>      學利    </FS2>";
            $content .= "\n";
            $content .= "地址 : ".$address."\n";
            $content .= "\t\t電話 : ".$phone."\n\n";
            $content .= "\t\t\t\t".date('y/m/d H:i')."\n";
            $content .= "桌號 : ".$orderinfo['table_num']."\n";
            $content .= "**********************************************\n";
            //$content .= "名称\t\t数量\t\t单价\n";

            foreach($goodsinfo as $key=>$list){
                $attrprice = 0;
                if(!empty($list['attrs'])){
                    $attrArr = unserialize($list['attrs']);
                    if($attrArr){
                        foreach($attrArr as $attr){
                            $attrprice += $attr['price'];
                        }
                    }
                }
                $list['price'] += $attrprice;
                $content .= $list['pname']."\tx".$list['quantity']."\t"."$".$list['price']."\n";
//                $content .= $list['pname']."\tx".$list['quantity']."\t"."$".$list['price']."\n";
                $specStr = str_replace(array($list['pname']." ",$list['pname']), '', $list['name']);
                $content .= empty($specStr) ? '' : $specStr."\n";
                if(!empty($list['attrs'])){
                    $attrs = unserialize($list['attrs']);
                    foreach ($attrs as $k => $val) {
                        $content .= $val['name']."x".$val['num']." ";
                    }
                    $content .="\n";
                }
                $content .="\n";

            }

            $content .= "點菜總額 : \t\t"."$".$orderinfo['total']."\n\n";
            $content .= "<FS2>總額    "."$".$orderinfo['total']." </FS2>";
            $content .= "\n";
            // $content .= "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
            // $content .= "客戶姓名 : ".$orderinfo['nickname']."\n";
            // $content .= "客戶電話 : ".$orderinfo['phone']."\n";
            // $content .= "送貨地址 : ".$orderinfo['address']."\n";
            // if($orderinfo['order_way']==1) {
            //     $content .= "支付方式 : " . "自取". "\n\n";
            // }else {
            //     $content .= "支付方式 : " . "paypal支付" . "\n\n";
            // }
            $content .= "<BR3>".$orderinfo['invoice_no']."</BR3>";
            $content .= "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
            $content .= "<center>多謝惠顧  請再光臨</center>";
            $content .= "<center>學利魚蛋  全港至爽</center>";
            //打印
            //exit($content);
            $this->yprint->action_print($printname,$printkey,$content);
           $this->orders_mdl->edit_orders(array('is_print'=>2),$order_id);

        }
    }
	/*打印当天订单总额*/
    public function dayTotal_get(){
        $this->load->model('orders_mdl');
        $today_begin = date("Y-m-d 00:00:00");
        $today_end = date("Y-m-d 23:59:59");
        $today=array(
            'begin_time'=>strtotime($today_begin),
            'end_time'=>strtotime($today_end),
        );
        $today_order=$this->orders_mdl->totle_orders($today);
        $total=$this->db->select_sum('total')->get_where('clickrcms_order','add_time BETWEEN "'.strtotime($today_begin).'" AND "'.strtotime($today_end).'"');
        if(!empty($total) && $total->num_rows()>0){
            $result=$total->row_array();
            if(empty($result['total'])){
                $today_total=0;
            }else{
                $today_total=$result['total'];
            }

        }
        $this->load->library('yprint');
        $lists = $this->get_list();
        foreach($lists as $key=>$arr){
            foreach($arr as $v){
                if($v['key']=='printname'){
                    $printname = $v['value'];
                }else if($v['key']=='printkey'){
                    $printkey = $v['value'];
                } else if($v['key']=='telephone'){
                    $phone = $v['value'];
                }else if($v['key']=='address'){
                    $address = $v['value'];
                }
            }

        }
        $content = "<FS2>          學利         </FS2>";
        $content .= "\n";
        $content .= "地址 : ".$address."\n";
        $content .= "\t\t電話 : ".$phone."\n\n";
        $content .= "**********************************************\n";
        $content .= "<FS2>本日結算</FS2>"."\n";
        $content .= "<FS2>"."日期 : ".date('d/m/Y',time())."</FS2>"."\n";
        $content .= "<FS2>本日總成交(單)</FS2>"."\n";
        $content .= "<FS2>".$today_order."</FS2>"."\n";
        $content .= "<FS2>本日收入總額(HKD)</FS2>"."\n";
        $content .= "<FS2>".$today_total."</FS2>"."\n";
        $content .= "<FS2>結算時間</FS2>"."\n";
        $content .= "<FS2>".date('d/m/Y H:i',time())."</FS2>"."\n";
        $content .= "**********************************************\n";
        $this->yprint->action_print($printname,$printkey,$content);
    }
    //獲取基本設置
    public function get_list(){
        $this->load->model('settings_mdl');
        $query=$this->settings_mdl->get_settings();
        $sdata=array();
        foreach($query->result_array() as $q){
            $sdata[$q['group']][]=array(
                'key'=>$q['key'],
                'value'=>$q['value'],
                'description'=>$q['description']);
        }
        $lists = $sdata;
        return $lists;
    }
    protected function _get_posprint($id=array()){
        if(!empty($id)){
            $this->db->where_in('sp.id', $id);
            $this->db->select('sp.id,sp.name');
            $this->db->from('clickrcms_specs_items AS sp');
            $list   = array();
            $result = $this->db->get();
            if($result->num_rows())
                $list = $result->result_array();
            return $list;
        }
    }

}